// $Id: aprob.cpp 14 2010-11-18 21:56:36Z dheater@heatertech.com $

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "linked_list_t.h"

#define MAX_LINE_SIZE 256
#define NUM_ENTITIES  10

typedef base::linked_list_t<unsigned int> list_t;
typedef list_t::node_t node_t;


void parse_line(list_t& elements)
{
    char line[MAX_LINE_SIZE];
    if(NULL == fgets(line, sizeof(line), stdin))
    {
        return;
    }
    printf("%s\n", line);

    /* Read the size of the input line.
     */
    unsigned int num_elements;
    int idx;
    sscanf(line, "%u%n", &num_elements, &idx);
    //printf("%u\n", num_elements);

    /* Create pointer to the begining of the line.
     */
    char* line_ptr = line;

    /* Read the data values.
     */
    for(unsigned int i; i < num_elements; ++i)
    {
        /* Move the start of the line up past the data already read.
         */
        line_ptr += idx;

        unsigned int value;
        sscanf(line_ptr, "%u%n", &value, &idx);
        printf("%d %u\n", idx, value);

        /* Store the data value into the linked list.
         */
        elements.add_back(value);
    }
}


int main(int argc, char *argv[])
{

    do {
        list_t raw_list;

        /* Read and parse a line of input and store it in the linked list.
         */
        parse_line(raw_list);

        /* If we got back an empty list, then we are at the end of the input
         * data.
         */
        if(NULL == raw_list.head())
        {
            return 0;
        }

        node_t* val_ptr;
        while((val_ptr = raw_list.head()) != NULL)
        {
            printf("%u ", val_ptr->data);
            raw_list.delete_front();
        }
        printf("\n");

    } while(true);

    return 0;
}
